package com.core.indexer;

import java.util.ArrayList;

public class BM25 {

	public static final double DEF_k1 = 1.2;
	public static final double DEF_b = 0.75;
	
	
	private double AL;
	private int Ld;
	private int N;
	private static BM25 mInstance = null;
	
	public static BM25 Get()
	{
		if(mInstance == null)
			mInstance = new BM25();
		
		return mInstance;
	}
	
	public void Init(ArrayList<IndexData.DocList> docList)
	{
		AL = 0;
		Ld = 0;
		N = docList.size();
		
		for(IndexData.DocList doc : docList)
		{
			Ld += doc.DocSize;
		}
		
		AL = Ld/docList.size();
	}
	
	public double GetScore(int DocSize, int ft, int fdt)
	{
		return GetScore(DocSize, ft, fdt, 1);
	}
	
	public double GetScore(int DocSize, int ft, int fdt, double wt)
	{
		double K_ = DEF_k1*((1 - DEF_b) + DEF_b*DocSize/AL);	
		return Math.log(wt*(N - ft + 0.5)/(ft + 0.5)) * ((DEF_k1 +1)*fdt/(K_+fdt));
	}
	private BM25(){}
}
